//
//  simItemSelectViewController.m
//  simens
//
//  Created by amy zhang on 12-7-24.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "simItemSelectViewController.h"
#import "simShopDetailController.h"
#import "simAddVisitInfoController.h"
#import "ASIHTTPRequest.h"
#import "JSONKit.h"

@interface simItemSelectViewController ()<UITableViewDelegate,UITableViewDataSource,ASIHTTPRequestDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSString *type;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) dictItem *selectedItem;
@property (nonatomic) NSInteger selectedRow;
@end

@implementation simItemSelectViewController
@synthesize tableView=_tableView;
@synthesize type=_type;
@synthesize dataArray=_dataArray;
@synthesize selectedItem=_selectedItem;
@synthesize selectedRow=_selectedRow;


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dataArray=[[NSMutableArray alloc] init];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 
                                                                   self.view.bounds.size.height) 
                                                  style:UITableViewStyleGrouped];  
    
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.view addSubview: self.tableView];
    
    UIBarButtonItem* tempRight = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(saveButtonPress)];
    self.navigationItem.rightBarButtonItem = tempRight;
    
    UIBarButtonItem* leftBt=[[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(changedModel)];
    self.navigationItem.leftBarButtonItem=leftBt;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    if([self.type isEqualToString:@"1"]==YES){
        [self showMbprogress];
        NSString* u=[NSString stringWithFormat:@"%@/%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"WebserviceUrl"],@"getSiemensDict?dictTypeValue=pro@siemens"]; 
        NSURL *url = [NSURL URLWithString:u];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setDelegate:self];
        [request start];
        
    }else if([self.type isEqualToString:@"2"]==YES){
        [self showMbprogress];
        NSString* u=[NSString stringWithFormat:@"%@/%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"WebserviceUrl"],@"getSiemensDict?dictTypeValue=prostatus@siemens"]; 
        NSURL *url = [NSURL URLWithString:u];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setDelegate:self];
        [request start];
        
    }
    
}
-(void)setType:(NSString *)type title:(NSString *)t itemid:(NSString *)v{
    self.type=type;
    self.title=t;
    self.selectedItem=[[dictItem alloc] init];
    self.selectedItem.itemid=v;
}

-(void) saveButtonPress{
    simAddVisitInfoController *addController=[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];
    [addController setData:self.selectedItem];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] animated:YES];
    
}

-(void) changedModel {
    [self.navigationController popViewControllerAnimated:YES];   
}
- (void)requestFinished:(ASIHTTPRequest *)request {
    NSError *error = [request error];
    
    if (!error) {
        NSString *response = [request responseString];
        NSLog(@"load data is :%@",response);
        NSDictionary* dict = [response objectFromJSONString];
        NSArray * dictArray=[dict objectForKey:@"siemensPro"];
        if(dictArray!=nil){
            
            for(int i=0;i<dictArray.count;i++){
                NSDictionary* temp = [dictArray objectAtIndex:i];
                dictItem *d=[[dictItem alloc] init];
                d.itemname=[temp objectForKey:@"itemName"];
                d.itemid=[[temp objectForKey:@"itemId"] stringValue];
                [self.dataArray addObject:d];
                //判断上次选中的项
                if(self.selectedItem.itemid!=nil){
                    if([d.itemid isEqualToString:self.selectedItem.itemid]==YES){
                        self.selectedRow=i;
                    }  
                }
            }
            
        }
    }
    [self hideMbProgress];
    [self.tableView reloadData];
}
    
- (void)requestFailed:(ASIHTTPRequest *)request {
        [self hideMbProgress];
    }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}// Default is 1 if not implemented


//tableview行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString* CellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section, indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    } 
    
    for (UIView *v in [cell subviews]) {
        if ([v isKindOfClass:[UILabel class]] |
            [v isKindOfClass:[UIButton class]]) {
            [v removeFromSuperview];
        }
    }
    
   
       dictItem *item=[self.dataArray objectAtIndex:indexPath.row];

        UILabel* nameLabel =[[UILabel alloc] initWithFrame:CGRectMake(20, 5, 290, 30)];
        [nameLabel setBackgroundColor:[UIColor clearColor]]; 
        [nameLabel setText:item.itemname]; 
        nameLabel.font=[UIFont systemFontOfSize:16];
        if([self.type isEqualToString:@"2"]){
            if(indexPath.row==0){
                [nameLabel setTextColor:[UIColor redColor]];
            }else if(indexPath.row==1){
                [nameLabel setTextColor:[UIColor blueColor]];
            }else if(indexPath.row==2){
                [nameLabel setTextColor:[UIColor blackColor]];
            }
            else if(indexPath.row==3){
                [nameLabel setTextColor:[UIColor greenColor]];
            }
            else if(indexPath.row==4){
                [nameLabel setTextColor:[UIColor brownColor]];
            }
            else if(indexPath.row==5){
                [nameLabel setTextColor:[UIColor orangeColor]];
            }
        }
        [cell addSubview:nameLabel];
    
    if(self.selectedItem.itemid!=nil)
    if(indexPath.row==self.selectedRow){
        cell.accessoryType=UITableViewCellAccessoryCheckmark;//勾勾
        self.selectedItem=item;       
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
        
    }
   
    return cell;
    
}

//row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return @"";
}






- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
      cell.accessoryType=UITableViewCellAccessoryCheckmark;//勾勾
    self.selectedItem=[self.dataArray objectAtIndex:indexPath.row];
    //NSLog(@"select:%@ %@",self.selectedItem.itemname,self.selectedItem.itemid);
}

-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType=UITableViewCellAccessoryNone; 
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
